package com.itc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.bean.BeanUtils;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.system.api.domain.SysDictData;
import com.itc.system.domain.dto.SysDictDataDto;
import com.itc.system.service.SysDictDataService;
import com.itc.system.mapper.SysDictDataMapper;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author zhangyh
* @description 针对表【sys_dict_data(字典数据表)】的数据库操作Service实现
* @createDate 2024-05-08 14:27:27
*/
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData>
    implements SysDictDataService{

    @Override
    public R dictDataAdd(SysDictDataDto dict) {
        //校验当前类型下的字典数据是否存在
        Long count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<SysDictData>()
                .eq(SysDictData::getDictType,dict.getDictType())
                .eq(SysDictData::getDictLabel,dict.getDictLabel())
        );
        if(count > 0){
            return R.fail("字典标签重复");
        }
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(dict,sysDictData);
        sysDictData.setCreateBy(SecurityUtils.getUsername());
        sysDictData.setCreateTime(LocalDateTime.now().toString());
        this.baseMapper.insert(sysDictData);
        return R.ok();
    }

    @Override
    public R dictDataUpdate(SysDictDataDto dict) {
        SysDictData sysDictData = this.baseMapper.selectById(dict.getDictCode());
        if(sysDictData == null){
            return R.fail("字典数据信息异常");
        }
        BeanUtils.copyProperties(dict,sysDictData);
        this.baseMapper.updateById(sysDictData);
        return R.ok();
    }

    @Override
    public R dictDataDelete(List<Long> dictCodes) {
        this.baseMapper.deleteBatchIds(dictCodes);
        return R.ok();
    }
}




